/*
 * @Author: zhangminhong zhangminhong@zhujia360.com
 * @Date: 2023-03-09 10:48:10
 * @LastEditors: zhangminhong zhangminhong@zhujia360.com
 * @LastEditTime: 2023-03-09 11:16:50
 * @FilePath: /zhujiayun-website-manage/Users/zhang/Desktop/zhang/gitZMH/zhang-cli/config/webpack.dev.config.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
var merge = require('webpack-merge');
var common = require('./webpack.base.config');
var webpack = require('webpack');
var path = require('path');
import openBrowser from 'react-dev-utils/openBrowser';
import DefaultConfig from '../src/config.default'
let ip = require('ip')
let host = DefaultConfig.listen.host == "0.0.0.0" ? ip.address() : DefaultConfig.listen.host
let port = DefaultConfig.listen.port;
export default merge(common, {
    mode: 'development',
    entry: ['./src/main'],
    module: {
        rules: [
            {
                test: /\.tsx|ts|js|jsx$/,
                loader: 'esbuild-loader',
                exclude: '/node_modules/',
                options: {
                    loader: 'tsx',  // Remove this if you're not using JSX
                    target: 'es2015',
                },
                include: path.resolve(__dirname, '../src'),
            },
            {
                test: /\.(eot|ttf|svg|woff|woff2|doc|xlsx|xls|exe)$/,
                use: {
                    loader: 'file-loader',
                    options: {
                        // outputPath: 'font/'
                        esModule: false
                    }
                }
            },
            {
                test: /\.(css|less)$/,
                exclude: path.resolve(__dirname, '../src'),
                use: [
                    {
                        loader: 'style-loader',
                        options: {
                            esModule: true
                        }
                    },
                    'css-loader',
                    {
                        loader: 'less-loader',
                        options: {
                            javascriptEnabled: true
                        }
                    }
                ]
            },
            {
                test: /\.(less|css)$/,
                include: path.resolve(__dirname, '../src'),
                use: [
                    {
                        loader: 'style-loader',
                        options: {
                            esModule: true
                        }
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            modules: {
                                localIdentName: '[local]--[contenthash:base64:5]'
                            },
                        }
                    },
                    'postcss-loader',
                    {
                        loader: 'less-loader',
                        options: {
                            javascriptEnabled: true
                        }
                    }
                ],
            },
            {
                test: /\.(png|jpg|gif|jpeg)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192,
                            name: '[name].[contenthash].[ext]'
                        },
                    },
                ],
            },
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            BASE_URL: JSON.stringify(process.env.CROSSENV == 'test' ? DefaultConfig.request.prefix.test : DefaultConfig.request.prefix.prod),
            ABASE_URL: JSON.stringify(process.env.CROSSENV == 'test' ? 'https://test-ydjm.zje.com' : 'https://ydjm.zje.com')
        }),
        {
            apply(compiler: { hooks: { done: { tap: (arg0: string, arg1: () => void) => void; }; }; }) {
                let run = false
                // 在 compilation 完成时执行
                compiler.hooks.done.tap('open-browser', () => {
                    if (!run) {
                        openBrowser(`http://${host}:${port}`)
                        run = true
                    }
                })
            }
        },
    ],
    devtool: "source-map"
});